import cv2
import numpy as np


# 读取图片
def ReadImg():
    img = cv2.imread('image/test/q.jpg', 1)
    cv2.imshow('src', img)
    return img


# 高斯滤波
def GausBlur(src):
    dst = cv2.GaussianBlur(src, (5, 5), 1.5)
    cv2.imshow('GausBlur', dst)
    return dst


# 灰度处理
def Gray_img(src):
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    cv2.imshow('gray', gray)
    return gray


# 二值化
def threshold_img(src):
    ret, binary = cv2.threshold(src, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_TRIANGLE)#
    print("threshold value %s" % ret)
    # cv2.imshow('threshold', binary)
    return binary

def open_mor(src):
    kernel = np.ones((5, 5), np.uint8)
    opening = cv2.morphologyEx(src, cv2.MORPH_OPEN, kernel, iterations=3)  # iterations进行3次操作
    # cv2.imshow('open', opening)
    return opening

# Read images

src = cv2.imread("image/test/n.jpg")
dst = cv2.imread("image/test/3.JPEG")
cv2.imshow('src',src)
# print(type(src))
# Create a rough mask around the airplane.
# src_mask = np.zeros(src.shape, src.dtype)
src_mask = 255 * np.ones(src.shape, src.dtype)


dst_mask = 255 * np.ones(dst.shape, dst.dtype)
#
# mask = 255 * np.ones(obj.shape, obj.dtype)


# src = ReadImg()
gaus_img = GausBlur(src)
gray_img = Gray_img(gaus_img)
thres_img = threshold_img(gray_img) #这就是alpha
open_img=open_mor(thres_img)
# cv2.imshow('open_img',open_img)
reverse_alpha = cv2.bitwise_not(thres_img)  # 反转
img_without_green = cv2.bitwise_and(src, src, mask=thres_img)  #Alpha取图像
# cv2.imshow('img_without_green',img_without_green)


#
# poly = np.array([[4,80], [30,54], [151,63], [254,37], [298,90], [272,134], [43,122]], np.int32)
# cv2.fillPoly(src_mask, [poly], (255, 255, 255))
#
# # 这是 src CENTER 所在的地方
# # center = (800,100)
center=(300,370)
# # Clone seamlessly.
# output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)
output = cv2.seamlessClone(src, dst, src_mask, center,cv2.MIXED_CLONE)#cv2.NORMAL_CLONE\cv2.MONOCHROME_TRANSFER\cv2.MIXED_CLONE
width, height, channels = dst.shape
# center1 = (int(height/2), int(width/2))
# output1= cv2.seamlessClone(dst, src, dst_mask, center1,cv2.MIXED_CLONE)
# # print(type(output))
# # rout=cv2.imread(output)
cv2.imshow('img',output)
cv2.waitKey()
cv2.destroyAllWindows()
# 保存结果
# cv2.imwrite("image/seamOut/out.jpg", output);